#ifndef BADPIXELDETECTOR_H
#define BADPIXELDETECTOR_H

#include <QtGui/QMainWindow>
#include "ui_badpixeldetector.h"
#include "YK16GrayImage.h"

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkDerivativeImageFilter.h"
#include "itkSmoothingRecursiveGaussianImageFilter.h"
#include "itkIntensityWindowingImageFilter.h"
#include "itkMedianImageFilter.h"

//class YK16GrayImage;
//
//#define BLACK_VALUE 0
//#define NORMAL_VALUE 1000
//#define WHITE_VALUE 4095
//#define CENTERLINE_VALUE 30000
//
//#define DEFAULT_CROSSHAIR_MARGIN 20
//#define DEFAULT_SAMPLING_PIXELS 3
//#define DEFAULT_ROI_RATIO_X 0.5
//#define DEFAULT_ROI_RATIO_Y 0.5
#define DEFAULT_PERCENT_BADPIX_ON_COLUMN 30
#define DEFAULT_PERCENT_BADPIX_ON_ROW 30

typedef itk::Image<float,2> FloatImageType;
typedef itk::Image<unsigned short, 2> UnsignedShortImageType;
typedef itk::ImageFileReader<UnsignedShortImageType> readerType;
typedef itk::DerivativeImageFilter<UnsignedShortImageType, FloatImageType> DerivativeFilterType;
typedef itk::SmoothingRecursiveGaussianImageFilter<UnsignedShortImageType, UnsignedShortImageType>  SmoothingFilterType;
typedef itk::IntensityWindowingImageFilter<UnsignedShortImageType, UnsignedShortImageType>  WindowingFilterType;
typedef itk::MedianImageFilter<UnsignedShortImageType,UnsignedShortImageType> MedianFilterType;


using namespace std;


struct PIXINFO{
	int infoX;
	int infoY;
	unsigned short pixValue;
};



class BadPixelDetector : public QMainWindow
{
	Q_OBJECT

public:
	BadPixelDetector(QWidget *parent = 0, Qt::WFlags flags = 0);
	~BadPixelDetector();
	void LoadBadPixelMap(const char* filePath);
	int AddBadPixLine(vector<BADPIXELMAP>& vPixelReplMap, int direction);//direction == 0: hor bad pix row, directino ==1: ver bad pix column
	//void DetectBadPixels(vector<BADPIXELMAP>& vPixelReplMap);//detect bad pixels and add it to the vector//vector will not cleared
	void DetectBadPixels(vector<BADPIXELMAP>& vPixelReplMap, bool bRefresh);

	public slots:		
		void SLT_LoadDarkImage();
		void SLT_LoadGainImage();
		void SLT_DrawDarkImage();
		void SLT_DrawGainImage();
		void SLT_ShowBadPixels();
		void SLT_DetectBadPixels();
		void SLT_SavePixelMap();
		void SLT_UncorrectDark(); //Gain Image + Dark
		void SLT_DoReplacement_Dark();
		void SLT_DoReplacement_Gain();		
		void SLT_LoadBadPixelMap();
		void SLT_SaveCurDark();
		void SLT_SaveCurGain();
		void SLT_AccumulateBadPixels();
		/*void SLT_GoToOriginal();}
		void SLT_Median();
		void SLT_Gaussian();
		void SLT_DerivativeHor();
		void SLT_DerivativeVer();		
		void SLT_RemoveCrosshairHor();
		void SLT_RemoveCrosshairVert();
		void SLT_SaveAs();
		void SLT_SetCurImageAsNewSourceImage();
		void SLT_RemoveCrosshairMacro();*/


public:
	//unsigned short GetSubstituteVert(int x,int y, double coeff0, double coeff1, int halfThickness, int iSamplePixels, unsigned short* medianImg, int width, int height);
	//unsigned short GetSubstituteHor(int x,int fixedY, double coeff0, double coeff1, int halfThickness, int iSamplePixels, unsigned short* medianImg, int width, int height);

	//bool SaveAutoFileName(QString& srcFilePath, QString endFix);


	

public:
	YK16GrayImage* m_pImageYKDark;
	YK16GrayImage* m_pImageYKGain;
	
	QString m_strSrcFilePathDark;
	QString m_strSrcFilePathGain;	

	//UnsignedShortImageType::Pointer m_itkCurImage;
	//FloatImageType::Pointer m_spTarImage;

	//UnsignedShortImageType::Pointer m_spTarImageDisp;
	int m_iWidth;
	int m_iHeight;

	vector<BADPIXELMAP> m_vPixelReplMap;	

	double m_fPercentThre; //percent thershold for bad pixel detection with respect to difference between 

	int m_iMedianSize;
	//mean Gain - Dark pixel value

	//int m_iCrosshairMargin;

	//double m_fSearchROI_X; //if this value = 50, only cross-hair candidates in half region of the full image will be recognized as valid.
	//double m_fSearchROI_Y;


private:
	Ui::BadPixelDetectorClass ui;
};

#endif // BADPIXELDETECTOR_H